<?php


namespace App\Http\Repositories\Base;


use App\Exports\InvoicesExport;
use App\Http\Repositories\BaseRepository;
use App\Imports\ImportsExcel;
use App\Jobs\ExportsCsvJob;
use App\Jobs\ExportsExcelJob;
use App\Models\UserModels\Models\UserModel;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;
use Maatwebsite\Excel\Facades\Excel;

class FileRepository extends BaseRepository
{
    private $image;
    private $storage;

    public function __construct(Image $image, Storage $storage)
    {
        $this->image = $image;
        $this->storage = $storage;
    }

    /**
     * 上传
     * @param $data
     */
    public function upload($data)
    {

        $file = is_array($data['file']) ? $data['file'] : [$data['file']];

        foreach ($file as $v) {
            if ($v->isValid()) {
                $res['realpath'] = $v->getRealPath();
                $res['ext'] = $v->getClientOriginalExtension();
                $res['type'] = $v->getMimeType();
                $res['name'] = $v->getFileName();
                $res['title'] = $v->getClientOriginalName();
                $res['md5'] = md5_file($res['realpath']) . '.' . $res['ext'];
                //上传
                $this->storage::put(date('Ymd') . '/' . $res['md5'], file_get_contents($res['realpath']));
            }
        }

        return $res;
    }

    public function addData($data)
    {

    }

    public function excel($data)
    {


    }

    public function a($a, $b )
    {
        dd($a,$b);
    }

    public function b($b)
    {
        echo $b ?? '321';
    }

    public function getFun($class, $function, $a, $b)
    {
        call_user_func([$class, $function], $a, $b);
    }

    public function export($data)
    {
        $cache = new Redis();
//        $cache::set('123',123);
        $cache::sAdd('a', 1);
        $cache::sAdd('a', 2);
        $cache::sAdd('a', 3);
        print_r($cache::sMembers('a'));

        dd(1);
//        return $cache::sMembers('a');






        $a = [1,2,3,4,5,6,7,8,9,0];
        shuffle($a);
        $a = array_slice($a, 0, 5);
        dd($a);




        $function = 'a';
        $a = 'a';
        $b = 'b';
//        $this->{$function}($a);

        $this->getFun($this, $function, $a,$b);
        dd(1);




        $format = [
            'id',
            'role_id',
            'member_name',
            'member_password',
            'member_uuid',
            'member_token',
            'member_email',
            'member_phone',
            'status',
            'create_time',
            'update_time',
            'is_delete',
        ];
        $userModel = new UserModel();
//        Cache::set('b', $userModel->limit(50000)->get()->toArray());
//        dd(1);
        $arr = Cache::get('a');
//        dd($arr[0]);
//        var_dump($arr[0]);exit;

       return
           $export = (new InvoicesExport($arr, $format, 'A1'))
        ->download('2.xls')
        ;
        Excel::store($export, '526.csv', 'local');
        dd(1);
//        dd($a[0]);
//        $arr = $userModel->where('id',1)->limit(1)->get();
//        dd($arr);
        $excel = (new InvoicesExport($arr, $format, 'A1'))
            ->queue('invoices.xlsx', 'local')
            ->allOnQueue('export');
//        $excel->store('123.csv');


         dd(1);
//        $job = new ExportsCsvJob($arr, 'xin1', $format);
//        dispatch($job->onQueue('export'));

        return 1;

    }

    public function exportExcel($data)
    {
        $format = [
            'id',
            'role_id',
            'member_name',
            'member_password',
            'member_uuid',
            'member_token',
            'member_email',
            'member_phone',
            'status',
            'create_time',
            'update_time',
            'is_delete',
        ];

        $userModel = new UserModel();
        $curr = 1;
        $arr = $userModel->limit(100)->get()->toArray();
        $job = new ExportsExcelJob($arr, 'xin1', $format, 'A1', 'csv');
        dispatch($job->onQueue('export'));
        $arr = $userModel->offset(100)->limit(100)->get()->toArray();
//        dd($arr[0]);
        $job = new ExportsExcelJob($arr, 'xin', $format, 'A101', 'csv');
        dispatch($job->onQueue('export'));

//        $userModel->limit(50)->chunk(10,function ($user) use ($format, &$curr){
//            $job = new ExportsExcelJob($user->toArray(), 'xin', $format, 'A'.$curr, 'csv');
//            dispatch($job->onQueue('export'));
//            $curr += 1;
//            dump($curr);
//        });

        return 'success';
//        $data = (new UserModel())
//            ->limit(50000)
//            ->get()
//            ->toArray();
        $data = Cache::get('arr');
//        dd($data[0]);
//        dd(count($data));
        $format = [
            'id',
            'role_id',
            'member_name',
            'member_password',
            'member_uuid',
            'member_token',
            'member_email',
            'member_phone',
            'status',
            'create_time',
            'update_time',
            'is_delete',
        ];

//        $export = new InvoicesExport($data, $format);

//        dispatch(new ExportsExcelJob($data, 'xin', $format))->onQueue('export');
//        (new ExportsExcelJob($data, '123', $format, 'A1', 'csv'))::dispatch();
//        (new InvoicesExport)->queue('invoices.xlsx')->onQueue('ExportsExce');
//
//        return back()->withSuccess('Export started!');
//        return Excel::download($export, '111.csv');

        return ['success'];
    }

    public function exportCsv($data)
    {
        $format = [
            'id',
            'role_id',
            'member_name',
            'member_password',
            'member_uuid',
            'member_token',
            'member_email',
            'member_phone',
            'status',
            'create_time',
            'update_time',
            'is_delete',
        ];

//        $userModel = new UserModel();
//        $arr = $userModel->limit(50000)->get()->toArray();
        $arr = Cache::get('a');

        $job = new ExportsCsvJob($arr, 'xin1', $format);
        dispatch($job->onQueue('export'));

        return 1;
    }
}

function a()
{
    echo '123';
}
